package roborally.Pieces;
import be.kuleuven.cs.som.annotate.*;

/**
 * This is a class representing a Game Piece. Since the position can also be a PositionWithOrientation, the only characteristic ascribed to 
 * this class is whether the object is terminated.
 * @invar The position is valid 
 * 		| Position.isValidPosition(getPosition().getX(), getPosition().getY(), getPosition().getBoard() )
 * @version 2.0
 * @author Bart Opsomer 2de Bachelor Ingenieurswetenschappen Electrotechniek-Computerwetenschappen 
 * 		   Peter Opsomer 1ste Master Wiskundige IngenieursTechnieken
 */
public abstract class GamePiece {

	private boolean terminated;
	/**
	 * Creates a Gamepiece which is not located at any position on any Board: use method setPosition 
	 * or setPositionWithOrientation for this.
	 * 
	 * @post | !new.isTerminated()
	 */
	@Raw @Model
	protected GamePiece() {
		this.terminated = false;
	}
	/**
	 * @return true if this GamePiece is terminated and ready for garbage-collection
	 */
	@Basic
	public boolean isTerminated() {
		return this.terminated;
	}
	/**
	 * Terminate this GamePiece and make it ready for garbage-collection. This method should be overridden by subclasses such as Robot
	 * 
	 * @post | new.isTerminated()
	 */
	public void terminate() {
		this.terminated = true;
	}
	/**
	 * @return the Position of this GamePiece on a Board
	 */
	public abstract Position getPosition();
	/**
	 * Test whether (x,y) is a valid position of any GamePiece. This method just calls Position.isValidPosition but can be addressed 
	 * faster by subclasses this way.
	 * @param 	position the supposed position of a GamePiece, may be null
	 * @return 	whether the given Position is a valid position for any GamePiece, which is also true if position is not effective 
	 * 			because it is possible that Walls, Batteries and Robots are not placed on any Board
	 * 			| (position == null) || Position.isValidPosition(position.getBoard(),position.getX(), position.getY() )
	 */
	public static boolean isValidPosition(Position position) {
		if(position == null) {
			return true;
		}
		return Position.isValidPosition(position.getBoard(),position.getX(), position.getY() );
	}
	/**
	 * Return a String to be added to the toString()-methods of subclasses. This method is not abstract, but subclasses should overwrite this
	 * method to get a representative name for them in the GUI. This method just gives info about the position of the GamePiece.
	 * 
	 * @return | if getPosition() == null) then 
	 * 		   |	result.endsWith(" not located on any Board")
	 * 		   | else 
	 * 		   |	result.endsWith(" at position (" + getPosition().getX() + "," + getPosition().getY() + ")")
	 */
	public String toString(){
		if(getPosition() == null)
			return " not located on any Board";
		return " at position (" + getPosition().getX() + "," + getPosition().getY() + ")";
	}
}
